package com.huiquan.management.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.management.dao.CompanyDao;
import com.huiquan.management.domain.Company;

/**
 * ClassName:Constants Description:
 *
 * @author ChaoKai Wen email:wenchaokai@gmail.com
 * @Date 14/9/22
 * @since Ver 1.0
 */
@Service("companyService")
public class CompanyService extends BaseService {

    @Autowired
    private CompanyDao companyDao;

    public ModelAndView list() {
        List<Company> companyList = companyDao.retrieveList();

        Map<String, Object> map = new HashMap<>();
        map.put("list", companyList);
        return new ModelAndView("management/index", map);
    }

    public ReturnData updateCompany(long id, User user, String type, String value) {
        Company company = new Company();
        company.setId(id);
        switch (type) {
            case "1":
                company.setName(value);
                break;
            case "2":
                company.setType(value);
                break;
            case "3":
                company.setContractFlag(value);
                break;
            case "4":
                company.setFixedAmountCount(Double.parseDouble(value));
                break;
            case "5":
                company.setBonusCount(Double.parseDouble(value));
                break;
            case "6":
                company.setLabelScale(Double.parseDouble(value));
                break;
            case "7":
                company.setCheckScale(Double.parseDouble(value));
                break;
            case "8":
                company.setCheckPersonNum(Integer.parseInt(value));
                break;
            case "9":
                company.setCheckTrackAccuracy(Double.parseDouble(value));
                break;
            case "10":
                company.setLabelTrackAccuracy(Double.parseDouble(value));
                break;
            case "11":
                company.setSentenceLimit(Integer.parseInt(value));
                break;
            default:
                break;
        }
        companyDao.update(company);

        return ReturnUtil.success();
    }

    public ReturnData delete(long id) {
        Company company = new Company();
        company.setId(id);
        companyDao.delete(company);
        companyDao.updateUserCompany(company);
        return ReturnUtil.success();
    }

    public ReturnData add(long id, String name, String type, String contractFlag) {
        Map<String, Object> param = new HashMap<>();
        param.put("name", name);
        int count = companyDao.retrieveSize(param);
        if (count > 0) {
            ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
            rc.setDesc("存在公司名称相同的记录");
            return ReturnUtil.fail(rc);
        }
        Company companyValue = new Company();
        companyValue.setContractFlag(contractFlag);
        companyValue.setName(name);
        companyValue.setType(type);
        companyDao.insert(companyValue);
        return ReturnUtil.success();
    }

}
